
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using Swaf.Logging;

namespace Swaf.Container
{
	/// <summary>
	/// This is the base class for expiration node implementations. 
	/// </summary>
	public abstract class ExpireNodeBase : INameAddressSupport, IExpireNode
	{
		protected bool m_isExpired = false;
		protected object m_value;
		protected INameAddressSupport m_container=null;
		public ExpireNodeBase()
		{
		}
		#region INameAddressSupport Members
		public virtual void put(string nameAddress, object val)
		{
			if(nameAddress == null)
				m_value = val;
			else if(m_value != null && m_value is INameAddressSupport)
				((INameAddressSupport)m_value).put(nameAddress, val);
		}

		void Swaf.Container.INameAddressSupport.put(string nameAddress, object val, object extraInfo)
		{
			put(nameAddress, val);
		}

		public object get(string nameAddress, object oneArg)
		{
			return this.get(nameAddress, new object[]{oneArg});
		}

		public object get(string nameAddress)
		{
			return this.get(nameAddress, new object[]{});
		}

		public virtual object get(string nameAddress, params object[] infoArgs)
		{
			if(nameAddress == null && !(m_value is INameAddressSupport))
				return m_value;
			else if(m_value != null && m_value is INameAddressSupport)
				return ((INameAddressSupport)m_value).get(nameAddress, infoArgs);
			return null;
		}

		public bool isGetOnly { get{return false;}}

		public XmlSerializationSupport SerializationSupport
		{get{return XmlSerializationSupport.None;}}

		public string Xml {get{return null;}}

		public string getXml(string options) {return null;}

		public bool containsField(String nameAddress)
		{
			if(nameAddress != null && m_value != null && m_value is INameAddressSupport)
				return ((INameAddressSupport)m_value).containsField(nameAddress);
			return false;
		}

		#endregion

		#region IExpireNode Members

		public virtual void clear()
		{
			bool cancel=false;
			if (m_container != null && m_container is ContainerBase)
			{
				((ContainerBase) m_container).raiseNodeExpiringEvent(m_value, ref cancel);
			}
			else
			{
				try
				{
					if(m_value is IDisposable)
						((IDisposable)m_value).Dispose();
				}
				catch(Exception)
				{
					Application.currentApp.Trace.write(TraceLevels.Verbose,"Unable to dispose of object {0} from ExpireNode.", m_value.GetType().Name);
				}
			}
			if (! cancel)
			{
				m_value = null;
				m_isExpired = true;
			}
		}

		public virtual void check()
		{
			// TODO:  Add ExpireNodeBase.check implementation
		}

		// CUSP00045879 new property added to interface to tell when expiration has occurred
		public bool isExpired
		{
			get { return m_isExpired; }
		}

		public INameAddressSupport container
		{
			get
			{
				return m_container;
			}
			set
			{
				m_container=value;
			}
		}

		#endregion
		public IEnumerator GetEnumerator() { return null; }
	}
}
